/**
 * 
 */
var roleFlag;
var winHeight = Ext.util.Cookies.get('winHeight'), formHeight = 318;
if (winHeight != null) {
	formHeight = (winHeight - 280) / 2;
}

Ext.define('Massage', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id',
						type : 'int'
					}, {
						name : 'myid'
					}, {
						name : 'title',
						type : 'string'
					}, {
						name : 'sendername'
					}, {
						name : 'sendtime'
					}, {
						name : 'mystatus'
					}]
		});
Ext.define('Task', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id'
					}, {
						name : 'title'
					}, {
						name : 'feedbacktime'
					}, {
						name : 'managername'
					}, {
						name : 'isread'
					}]
		});
var taskStore = Ext.create('Ext.data.Store', {
			model : 'Task',
			pageSize : 8,
			proxy : {
				type : 'ajax',
				url : 'taskAction!queryPage.action?part=demo',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});

function readTask(data, cell, record, rowIndex, columnIndex, store) {
	var id = record.get('id');
	var title = data;
	var isread = record.get('isread');
	var style = 'color:#0000ff;font-size: 13px;cursor: pointer;';
	title = '<span style="' + style + '"  data="' + title
			+ '" onclick="openTaskDetail(' + id + ',' + isread + ')">' + title
			+ '</span>';
	return title;
}
function openTaskDetail(id, isread) {
	if (isread == 2) {
		Ext.Ajax.request({
					url : 'taskAction!read.action?id=' + id + '&role=shi',
					success : function(response) {
						Ext.util.Cookies.set('taskId', id);
						Ext.util.Cookies.set('flag', 'look');
						Ext.util.Cookies.set('role', 'shi');
						window.top.Utils.addPage('taskQuery' + id, '任务详情',
								'js/resources/business/task/taskFeedback.js');
					},
					failure : function(response, opts) {
						console.log('server-side failure with status code '
								+ response.status);
					}
				});

	} else {
		Ext.util.Cookies.set('taskId', id);
		Ext.util.Cookies.set('flag', 'look');
		Ext.util.Cookies.set('role', 'shi');
		window.top.Utils.addPage('taskQuery' + id, '任务详情',
				'js/resources/business/task/taskFeedback.js');
	}
	taskStore.reload();
}

var taskGrid = Ext.create('Ext.grid.Panel', {
	id : 'taskGrid',
	enableOverflow : true,
	store : taskStore,
	border : 0,
	selModel : Ext.create('Ext.selection.CheckboxModel'),
	columns : [{
				text : 'id',
				sortable : true,
				hidden : true,
				dataIndex : 'id'
			}, {
				text : '任务主题',
				sortable : true,
				width : 200,
				flex : 2,
				renderer : readTask,
				dataIndex : 'title'
			}, {
				text : '反馈人',
				sortable : true,
				flex : 1,
				dataIndex : 'managername'

			}, {
				text : '反馈时间',
				sortable : true,
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (value) {
						if (value) {
							var dates = value.substr(0, 10).split("-");
							var datetime = new Date(dates[0], dates[1] - 1,
									dates[2], value.substr(11, 2), value
											.substr(14, 2), value.substr(17, 2));
							return Ext.Date.format(datetime, 'Y年m月d日 H:i:s');
						}

					}

				},
				dataIndex : 'feedbacktime',
				flex : 2

			}],
	stripeRows : false,
	bbar : ['->', {
		text : '更多任务>>',
		enableToggle : true,
		toggleHandler : function(btn, pressed) {
			top.Utils.addPage('26', '任务管理',
					'js/resources/business/task/task4shi.js');
		}
	}],
	viewConfig : {
		forceFit : true
		// 让grid的列自动填满grid的整个宽度，不用一列一列的设定宽度。
	}
});
Ext.define('Notice', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id',
						type : 'int'
					}, {
						name : 'title'
					}, {
						name : 'publishtime'
					}]
		});
Ext.define('Board', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id',
						type : 'int'
					}, {
						name : 'title'
					}, {
						name : 'publishtime'
					}]
		});
var massageStore = Ext.create('Ext.data.Store', {
			model : 'Massage',
			pageSize : 8,
			proxy : {
				type : 'ajax',
				url : 'messageAction!queryPage.action?status=1',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});

function openMsgDetail(id, mystatus, myid) {
	if (mystatus == 1) {
		Ext.Ajax.request({
					url : 'messageAction!read.action?messid=' + myid,
					success : function(response) {
						massageStore.reload();
						Ext.util.Cookies.set('messageId', id);
						window.top.Utils
								.addPage('messageQuery' + id, '查看信息',
										'js/resources/business/message/messageQuery.js');
					},
					failure : function(response, opts) {
						console.log('server-side failure with status code '
								+ response.status);
					}
				});

	} else {
		Ext.util.Cookies.set('messageId', id);
		window.top.Utils.addPage('messageQuery' + id, '查看信息',
				'js/resources/business/message/messageQuery.js');
	}
}
var massageGrid = Ext.create('Ext.grid.Panel', {
	store : massageStore,
	id : 'massageGrid',
	border : 0,
	// selModel : Ext.create('Ext.selection.CheckboxModel'),
	stripeRows : true,
	columns : [{
				text : 'id',
				dataIndex : 'id',
				hidden : true
			}, {
				text : 'myid',
				dataIndex : 'myid',
				hidden : true
			}, {
				text : 'mystatus',
				dataIndex : 'mystatus',
				hidden : true
			}, {
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (record.get('mystatus') == 1) {
						return "<img src='images/message_new.png'/>";
					} else {
						return "<img src='images/message_read.png'/>";
					}

				},
				width : 28
			}, {
				text : '标题',
				sortable : true,
				dataIndex : 'title',
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					var id = record.get('id');
					var myid = record.get('myid');
					var mystatus = record.get('mystatus');
					var style_new = 'color:#0000ff;cursor: pointer;';
					var style_read = 'cursor: pointer;';
					if (mystatus == 1) {
						return '<span style="' + style_new + '"  data="'
								+ value + '" onclick="openMsgDetail(' + id
								+ ',' + mystatus + ',' + myid + ')">' + value
								+ '</span>';
					} else {
						return '<span style="' + style_read + '"  data="'
								+ value + '" onclick="openMsgDetail(' + id
								+ ',' + mystatus + ',' + myid + ')">' + value
								+ '</span>';
					}

				},
				flex : 2
			}, {
				text : '发送人',
				remoteSort : true,
				dataIndex : 'sendername',
				flex : 1
			}, {
				text : '发送时间',
				sortable : true,
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (value) {
						if (value) {
							var dates = value.substr(0, 10).split("-");
							var datetime = new Date(dates[0], dates[1] - 1,
									dates[2], value.substr(11, 2), value
											.substr(14, 2), value.substr(17, 2));
							return Ext.Date.format(datetime, 'Y年m月d日 H:i:s');
						}

					}

				},
				dataIndex : 'sendtime',
				flex : 2
			}],
	bbar : ['->', {
		text : '更多消息>>',
		// pressed: pluginExpanded,
		enableToggle : true,
		toggleHandler : function(btn, pressed) {
			top.Utils.addPage('myMessages', '我的消息',
					'js/resources/business/message/myMessages.js');
		}
	}],
	listeners : {
		itemdblclick : function(dataview, record, item, index, e) {
		}
	}
});

var noticeStore = Ext.create('Ext.data.Store', {
			model : 'Notice',
			pageSize : 8,
			proxy : {
				type : 'ajax',
				url : 'noticeAction!queryPage4All.action',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});
var boardStore = Ext.create('Ext.data.Store', {
			model : 'Board',
			pageSize : 8,
			proxy : {
				type : 'ajax',
				url : 'boardAction!queryPage.action?allFlag=1',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});
function readTitle(data, cell, record, rowIndex, columnIndex, store) {
	var id = record.get('id');
	var title = data;
	var style = 'color:#0000ff;cursor: pointer;';
	title = '<span style="' + style + '"  data="' + title
			+ '" onclick="openDetail(' + id + ')">' + title + '</span>';
	return title;
}
function openDetail(id) {
	Ext.util.Cookies.set('noticeid', id);
	window.top.Utils.addPage('noticeQuery' + id, '公告详情',
			'js/resources/business/notice/noticeQuery.js');
}
function readBoardTitle(data, cell, record, rowIndex, columnIndex, store) {
	var id = record.get('id');
	var title = data;
	var style = 'color:#0000ff;cursor: pointer;';
	title = '<span style="' + style + '"  data="' + title
			+ '" onclick="openBoardDetail(' + id + ')">' + title + '</span>';
	return title;
}
function openBoardDetail(id) {
	Ext.util.Cookies.set('boardid', id);
	window.top.Utils.addPage('boardQuery' + id, '宣传详情',
			'js/resources/business/board/boardQuery.js');
}
var noticeGrid = Ext.create('Ext.grid.Panel', {
	store : noticeStore,
	border : 0,
	// selModel : Ext.create('Ext.selection.CheckboxModel'),
	stripeRows : true,
	columns : [{
				text : 'id',
				dataIndex : 'id',
				hidden : true
			}, {
				text : '主题',
				remoteSort : true,
				renderer : readTitle,
				dataIndex : 'title',
				flex : 2
			}, {
				text : '发布时间',
				remoteSort : true,
				dataIndex : 'publishtime',
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (value) {
						if (value) {
							var dates = value.substr(0, 10).split("-");
							var datetime = new Date(dates[0], dates[1] - 1,
									dates[2], value.substr(11, 2), value
											.substr(14, 2), value.substr(17, 2));
							return Ext.Date.format(datetime, 'Y年m月d日 H:i:s');
						}

					}

				},
				flex : 1
			}],
	bbar : ['->', {
		text : '更多公告>>',
		// pressed: pluginExpanded,
		enableToggle : true,
		toggleHandler : function(btn, pressed) {
			top.Utils.addPage('a1', '公告',
					'js/resources/business/notice/notice4all.js');
		}
	}],
	listeners : {
		itemdblclick : function(dataview, record, item, index, e) {
		}
	}
});
var boardGrid = Ext.create('Ext.grid.Panel', {
	store : boardStore,
	border : 0,
	stripeRows : true,
	columns : [{
				text : 'id',
				dataIndex : 'id',
				hidden : true
			}, {
				text : '主题',
				remoteSort : true,
				renderer : readBoardTitle,
				dataIndex : 'title',
				flex : 2
			}, {
				text : '发布时间',
				remoteSort : true,
				dataIndex : 'publishtime',
				renderer : function(value, metadata, record, rowIndex,
						colIndex, store) {
					if (value) {
						if (value) {
							var dates = value.substr(0, 10).split("-");
							var datetime = new Date(dates[0], dates[1] - 1,
									dates[2], value.substr(11, 2), value
											.substr(14, 2), value.substr(17, 2));
							return Ext.Date.format(datetime, 'Y年m月d日 H:i:s');
						}

					}

				},
				flex : 1
			}],
	bbar : ['->', {
		text : '更多宣传>>',
		enableToggle : true,
		toggleHandler : function(btn, pressed) {
			top.Utils.addPage('board11', '宣传信息',
					'js/resources/business/board/board4all.js');
		}
	}],
	listeners : {
		itemdblclick : function(dataview, record, item, index, e) {
		}
	}
});
var msgTask = Ext.create('Ext.form.Panel', {
			id : 'msgTask',
			style : 'margin-bottom: 10px;',
			iconCls : 'icon-msg',
			title : '未读消息',
			collapsible : true,
			tools : [{
						type : 'refresh',
						handler : function(e, target, panel) {
							massageStore.reload();
							taskStore.reload();
						}
					}],
			height : formHeight,
			layout : 'fit',
			items : []
		});
var form = Ext.create('Ext.form.Panel', {
	margins : '5 5 5 5',
	autoScroll : true,
	layout : 'column',
	border : false,
	defaults : {
		layout : 'anchor',
		autoScroll : true,
		defaults : {
			anchor : '100%'
		}
	},
	items : [{
		columnWidth : 1 / 2,
		autoScroll : true,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 0 5px 5px',
		items : [{
			id : 'dailyWorkPanel',
			xtype : 'panel',
			autoScroll : true,
			style : 'margin-bottom: 10px;',
			bodyStyle : {
				background : 'url(images/writing.png) no-repeat scroll 100% 100%'
				// ,padding : '10px'
			},
			title : '常用工作',
			iconCls : 'icon-often',
			// closable:true,
			collapsible : true,
			height : formHeight,
			html : ''
		}, msgTask]
	}, {
		columnWidth : 1 / 2,
		autoScroll : true,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px',
		items : [{
					xtype : 'panel',
					style : 'margin-bottom: 10px;',
					title : '最新公告',
					iconCls : 'icon-notice',
					collapsible : true,
					tools : [{
								type : 'refresh',
								handler : function(e, target, panel) {
									noticeStore.reload();
								}
							}],
					height : formHeight,
					layout : 'fit',
					items : [noticeGrid]
				}, {
					xtype : 'panel',
					style : 'margin-bottom: 10px;',
					title : '信息宣传栏',
					iconCls : 'icon-board',
					collapsible : true,
					height : formHeight,
					layout : 'fit',
					tools : [{
								type : 'refresh',
								handler : function(e, target, panel) {
									boardStore.reload();
								}
							}],
					items : [boardGrid]
				}]
	}]
});
function openMyAcci() {
	Ext.util.Cookies.set('acciFlag', 'demo');
	window.top.Utils.addPage("5", "报告单管理",
			"js/resources/business/accident/accident.js")
}

// 项目结束时解封
setInterval(function countmy() {
	if (roleFlag != null && roleFlag != 1 && roleFlag != 2 && roleFlag != 3) {
		Ext.Ajax.request({
			url : 'accidentAction!countMy.action?status=1',
			success : function(response) {
				var res = response.responseText;
				res = Ext.JSON.decode(res);
				var countMy = res.countMy;
				str = '<div class="dailyworklink">'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("accidentAdd","新增报告单","js/resources/business/accident/accidentAdd.js")>报告单新增</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=openMyAcci()>我有'
						+ countMy
						+ '个草稿待处理</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("24","我的消息","js/resources/business/message/myMessages.js")>我的消息</a></ul>'
						+ '</div>'
				Ext.getCmp('dailyWorkPanel').body.update(str);
			}
		});
	}
}, 5000);
function ExternalRefresh() {
	Ext.Ajax.request({
		url : 'roleAction!queryUserRole.action',
		success : function(response) {
			var responseText = response.responseText;
			responseText = Ext.JSON.decode(responseText);
			roleFlag = responseText.roleFlag;
			var str;
			if (roleFlag == 1 || roleFlag == 2) {
				var str = '<div class="dailyworklink">'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("25","全市报告单","js/resources/business/accident/accident4leader.js")>全市报告单</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("19","公告管理","js/resources/business/notice/notice.js")>公告管理</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("23","消息发送","js/resources/business/message/messageSend.js")>消息发送</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("taskAdd","下达任务","js/resources/business/task/taskAdd.js")>下达任务</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("26","任务管理","js/resources/business/task/task4shi.js")>任务管理</a></ul>'
						+ '</div>'
				// + '<br/><div style="float: right;"><img style="float:
				// right;width:50%;" src="images/writing.jpg"></img></div>';
				// Ext.getCmp('massageGrid').setVisible(false);
				msgTask.add(taskGrid);
				taskStore.reload();
				msgTask.setTitle('未读的反馈任务');
				Ext.getCmp('dailyWorkPanel').body.update(str);

			} else if (roleFlag == 3) {
				var str = '<div class="dailyworklink">'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("25","全区报告单","js/resources/business/accident/accident4leader.js")>全区报告单</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("27","任务管理","js/resources/business/task/task4quxian.js")>任务管理</a></ul>'
						+ '<ul class="dailywork1">'
						+ '<a href="#" onclick=top.Utils.addPage("23","消息发送","js/resources/business/message/messageSend.js")>消息发送</a></ul>'
						+ '</div>'
				// Ext.getCmp('taskGrid').setVisible(false);
				msgTask.add(massageGrid);
				massageStore.reload();
				Ext.getCmp('dailyWorkPanel').body.update(str);
			} else {
				var str;
				Ext.Ajax.request({
					url : 'accidentAction!countMy.action?status=1',
					success : function(response) {
						var res = response.responseText;
						res = Ext.JSON.decode(res);
						var countMy = res.countMy;
						str = '<div class="dailyworklink">'
								+ '<ul class="dailywork1">'
								+ '<a href="#" onclick=top.Utils.addPage("accidentAdd","新增报告单","js/resources/business/accident/accidentAdd.js")>报告单新增</a></ul>'
								+ '<ul class="dailywork1">'
								+ '<a href="#" onclick=openMyAcci()>我有'
								+ countMy
								+ '个草稿待处理</a></ul>'
								+ '<ul class="dailywork1">'
								+ '<a href="#" onclick=top.Utils.addPage("24","我的消息","js/resources/business/message/myMessages.js")>我的消息</a></ul>'
								+ '</div>'
						Ext.getCmp('dailyWorkPanel').body.update(str);
					}
				});
			}
			// Ext.getCmp('taskGrid').setVisible(false);
			msgTask.add(massageGrid);
			massageStore.reload();

		},
		failure : function(response) {
		}
	});
	noticeStore.reload();
	boardStore.reload();
};
Ext.onReady(function() {
			Ext.create('Ext.container.Viewport', {
						layout : 'fit',
						items : [form]
					});

			ExternalRefresh();
		});